home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16723 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: crchh327.rich.bnr.ca!jobell
  2. From: jobell@bnr.ca (Bret Bieghler)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating an object via new with ONLY a pointer to the object
  5. Date: 11 Apr 1996 15:56:24 GMT
  6. Organization: Bell-Northern Research Ltd.
  7. Message-ID: <4kja38$d46@crchh327.rich.bnr.ca>
  8. References: <4kh07v$lno@crchh327.rich.bnr.ca> <4kifrt$lk1@ubszh.fh.zh.ubs.com>
  9. NNTP-Posting-Host: crchhd4a.rich.bnr.ca
  10.  
  11. In article <4kifrt$lk1@ubszh.fh.zh.ubs.com>,
  12. Ian Johnston (by ubsswop) <ian.johnston@ubs.com> wrote:
  13. >In article <4kh07v$lno@crchh327.rich.bnr.ca>, jobell@bnr.ca (Bret Bieghler) writes:
  14. >|> An interesting problem I've come across... I was wondering if this
  15. >|> is possible:
  16. >|> 
  17. [deleted]
  18. >
  19. >Give each CommadObject class a *static* function:
  20. >
  21. >class ExitCommand : public CommandObject
  22. >{
  23. >    // ...
  24. >    static CommandObject *makeNew();
  25. >};
  26. >
  27. >CommandObject *ExitCommand::makeNew()
  28. >{
  29. >    return new ExitCommand;
  30. >}
  31. >
  32. >
  33. >Now you can make a table (assoc array, whatever) of strings and pointers
  34. >to these functions.
  35. >
  36. >typedef CommandObject *(*MakeCmdObj)();
  37. >
  38. >MakeCmdObj funcptr = commandTable[command];
  39. >CommandObject *baseptr = (*funcptr)();
  40. >
  41. >
  42. >Ian
  43.  
  44. Thanks for those who replied.  Ian's method has been the most elegant
  45. solution to the problem.  My associative array contains strings and
  46. function pointers.  When I need an object associated with the string
  47. I get the function pointer and execute it.
  48.  
  49. Thank you very much Ian.
  50.  
  51. Regards,
  52.  
  53. Joseph Bell
  54.  
  55.  
  56. -- 
  57. Joseph A. Bell (NOT Bret Bieghler) jobell@bnr.ca
  58. Northern Telecom / Bell-Northern Research
  59. "What?  Evacuate now, in our moment of triumph?  Surely you overestimate their chances."
  60.